/*
 *  Copyright (C) 2010 MADSA.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author:  Jorge Pintado de Santiago
 *          
 */

package com.madsa.AugmentedCompass;

import java.text.DecimalFormat;

import com.sisof.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.view.View;

public	class DrawOnTop extends View{
    protected final Paint myArcPaint = new Paint(); 
    public boolean isDirection = false;
    private float escala = 0.005f;
    private float sx = 1.0f;
    private float sy = 1.0f;
    private double anguloDestino;
    private Drawable marcador_izq, marcador_dcha;
	private String text= "";

	public DrawOnTop(Context context, double ang) {
		super(context);
		anguloDestino = ang;

		DecimalFormat df = new DecimalFormat("0.00"); 
		text = "" + df.format(anguloDestino);
		
        this.myArcPaint.setARGB(150, 255, 130, 20); 
        this.myArcPaint.setAntiAlias(true);
        this.myArcPaint.setStyle(Style.STROKE);
        this.myArcPaint.setStrokeWidth(20); 
        updateOrientation(new float[] {0, 0, 0});
        
        this.marcador_dcha = context.getResources().getDrawable(R.drawable.marcador_brujula_dcha);
        this.marcador_izq = context.getResources().getDrawable(R.drawable.marcador_brujula_izq);

	}
	
	protected void onDraw(Canvas canvas) {
		
		marcador_izq.setAlpha(95);
		marcador_izq.setBounds(0, this.getHeight()-150, 200, this.getHeight());
		marcador_izq.draw(canvas);

		marcador_dcha.setAlpha(95);
		marcador_dcha.setBounds(this.getWidth()-200, this.getHeight()-150, this.getWidth(), this.getHeight());
		marcador_dcha.draw(canvas);

		
		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.BLACK);
		paint.setTextSize(30);
		
		
		DecimalFormat df = new DecimalFormat("0.00"); 
		canvas.drawText("" + df.format(bearing), 20, this.getHeight()-45, paint);
		canvas.drawText("" + text, this.getWidth()-110, this.getHeight()-45, paint);

		
		if (isDirection){
			this.myArcPaint.setARGB(150, 0, 255, 0);
			canvas.scale(sx, sy, this.getWidth()/2, this.getHeight()/2);
				sx+=escala;
				sy+=escala;
			if (sx>=1.35 || sx<=0.85){
				escala=-escala;
			}
		}else{
			canvas.save();
	        
			this.myArcPaint.setARGB(150, 255, 130, 20); 
			canvas.rotate(-bearing, this.getWidth()/2, this.getHeight()/2);

			paint.setStyle(Paint.Style.FILL);
			paint.setStrokeWidth(2);
			paint.setARGB(200, 255, 0, 0);
			Path path = new Path();
			path.moveTo(this.getWidth()/2, this.getHeight()/2-125);
			path.lineTo(this.getWidth()/2+25,this.getHeight()/2-110);
			path.lineTo(this.getWidth()/2, this.getHeight()/2-150);
			path.lineTo(this.getWidth()/2-25,this.getHeight()/2-110);
			path.close();
			canvas.drawPath(path, paint); 
			
			canvas.restore();
		}

        float angleArcPaint = 360; 
        new RectF();
		RectF arcRect = new RectF(this.getWidth()/2-100,
				this.getHeight()/2-100,
				this.getWidth()/2+100,
				this.getHeight()/2+100); 		  
		  
		canvas.drawArc(arcRect, -90, angleArcPaint,
						true, this.myArcPaint);
				
		invalidate();
	} 
	
	private float bearing=0;
	private float bearingAnt=180;
	float pitch = 0;
	float roll = 0;

	public void setBearing(float _bearing) {
		if (Math.abs((_bearing+90-anguloDestino)-bearingAnt)>4 && !(Math.abs(_bearing)<5)){
			bearing = (float) (_bearing+90-anguloDestino);
			bearingAnt = bearing;
			if (Math.abs(bearing)<7){
				isDirection=true;
			}else{
				isDirection=false;
			}
			if (bearing<0){
				bearing+=360;
			}
		}
	}
		
	public float getBearing() {
		return bearing;
	}

	public void setAnguloDestino(double ang){
		anguloDestino = ang;
	}
	
	public float getPitch() {
		return pitch;
	}
	public void setPitch(float pitch) {
		this.pitch = pitch;
	}
	public float getRoll() {
		return roll;
	}
	public void setRoll(float roll) {
		this.roll = roll;
	}

	
    private void updateOrientation(float[] values) {
    	setBearing(values[0]);
    	setPitch(values[1]);
    	setRoll(-values[2]);
    	invalidate();
    }
}